<?php

class PostTable extends DoctrineTable
{
  const
    STATUS_PUBLISHED       = 'PU',
    STATUS_PENDING         = 'PE';
    
  protected static
    $status                = array
                             (
                               self::STATUS_PUBLISHED => 'Published',
                               self::STATUS_PENDING   => 'Pending',
                             );
                             
  public function getStatuss()
  {
    return self::$status;
  }
  
  public function updateQueryForList(DoctrineQuery $q)
  {
    $q->innerJoin('p.User u');
  }
  
  public function findOneBySlug($slug)
  {
    $q = $this->createAliasQuery()
         ->innerJoin('p.PostIndex pi')
         ->where('p.slug = ?', $slug);
         
    return $q->fetchOne();
  }
  
  public function getQueryForLastPosts($limit)
  {
    $q = $this->createAliasQuery()
         ->leftJoin('p.Categories c')
         ->where('p.status = ?', self::STATUS_PUBLISHED)
         ->orderBy('p.datetime DESC')
         ->limit($limit);
         
    return $q;
  }
  
  public function findLastPosts($limit = 10)
  {
    return $this->getQueryForLastPosts($limit)->execute();
  }
  
  public function findLastPostsByCategorySlug($category_slug, $limit = 5)
  {
    $q = $this->getQueryForLastPosts($limit)->
         andWhere('c.slug = ?', $category_slug);
         
    return $q->execute();
  }
}
